/**
 * Crea un rectangulo.
 * @constructor
 * @param {Number} x - pos x.
 * @param {Number} y - pos y.
 * @param {Number} w - anchura.
 * @param {Number} h - altura.
 * @param {Color} color - color.
 */
Rectangle = function(x, y, w, h, color) {
    if (x == null || y == null || w == null || h == null) {
        var errorMsg = "Invalid rectangle parameters!";
        alert(errorMsg);

        throw new Error(errorMsg);
    }

    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.width = w;
    this.height = h;

    this.color = (color) ? color : new Color();
};

Rectangle.prototype.Contains = function(x, y) {
    if (x >= this.x && x <= this.x + this.w && y >= this.y && y <= this.y + this.h) {
        return true;
    }

    return false;
};

Rectangle.prototype.contains = Rectangle.prototype.Contains;

/**
 *  Retorna un vector que apunta al centro de la figura.
 *  */
Rectangle.prototype.GetCenterVec = function() {
    var mx = (this.x + this.w) / 2;
    var my = (this.y + this.h) / 2;
    return new Vector2(mx, my);
};

/**
 *  Retorna un vector diferencia entre el vector central de shape y el vector central this.
 *  */
Rectangle.prototype.GetRelVec = function(shape) {
    var mthis = this.GetCenterVec();
    var mthat = shape.GetCenterVec();

    return mthis.GetDiffVec(mthat);
};

Rectangle.prototype.Intersects = function(shape) {
    var retVal = 0;
    var offset = 0;
    if (shape.radius != null) {
        /*si se trata de un circulo entonces se calculara la interseccion como si fuese un circulo*/
        offset = shape.radius;
        if (this.Contains(shape.x - offset, shape.y - offset) || this.Contains(shape.x + shape.width - offset, shape.y - offset) || this.Contains(shape.x - offset, shape.y + shape.height - offset) || this.Contains(shape.x + shape.width - offset, shape.y + shape.height - offset)) {
            retVal = 1;
        } else if (shape.Contains(this.x - offset, this.y - offset) || shape.Contains(this.x + this.w - offset, this.y - offset) || shape.Contains(this.x - offset, this.y + this.h - offset) || shape.Contains(this.x + this.w - offset, this.y + this.h - offset)) {
            retVal = 1;
        }
    } else {
        /*si no es un circulo, se trata de un cuadrado*/
        retVal = checkRectCollision(this, shape);
    }


    return retVal;
};

Rectangle.prototype.intersects = Rectangle.prototype.Intersects;

Rectangle.prototype.Draw = function(ctx) {
    ctx.fillStyle = this.color.ToStandard();
    ctx.fillRect(this.x, this.y, this.w, this.h);
};

Rectangle.prototype.draw = Rectangle.prototype.Draw;

Rectangle.prototype.isOnTopOf = function(rect) {

};
